package com.ycx.login.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        System.out.println("==========>>>>sessionId:" + session.getId());
        System.out.println("======SESSION::::" + session);

        Object username = session.getAttribute("username");
        if(username == null) {
            // 没有登录,重定向到登录页
            response.sendRedirect(request.getContextPath() + "/login/toLogin");
            return false;
        }else{
            // 已登录，放行
            return true;
        }
    }
}
